jQuery(function($) {
	var doc = document,
		doc_ = $(doc);

	/*图片轮播*/
	var M = {},
		doc = document;

	M.create = function(elm) {
		return doc.createElement(elm);
	};

	M.Slide = function() {
		var that = this;
		that.frame = doc.querySelector(".slide");
		that.list = doc.querySelectorAll(".slide .nav li");
		that.images = doc.querySelectorAll(".gallery li");
		that.slideBox = doc.querySelector(".slide");
		that.navBox = doc.querySelector(".slide .nav");
		that.len = that.navBox.querySelectorAll("li").length;
		that.pre = doc.querySelector(".pre");
		that.next = doc.querySelector(".next");
		that.timer = 0;
		that.index = 0;
		
	};
	
	slide = M.slide = new M.Slide();
	
	slide.setIndex = function(list) {
		var that = this,
			len = list.length,
			i = 0;
			
		for (i; i < len; i++) {
			list[i].index = i;
			
		}
		list[0].className = "active";
		that.len = len;
	};
	

	slide.appendElm = function() {
		var that = this,
			navBox = that.navBox,
			images = that.images,
			len = that.images.length,
			i = 0,
			fragment = doc.createDocumentFragment();
			
		for (i; i < len; i++) {
			fragment.appendChild(M.create("li"));
		}
		navBox.appendChild(fragment);
		slide.setIndex(doc.querySelectorAll(".slide .nav li"));
	};
	slide.appendElm();
	
	slide.reElement = function(index) {
		var that = this,
			images = that.images,
			len = images.length,
			list = doc.querySelectorAll(".slide .nav li"),
			i = 0;
			
		for (i; i < len; i++) {
			images[i].className = "fadeout";
			list[i].className = "";
		}

		images[index].className = "fadein";
		list[index].className = "active";

	};
	
	slide.eventHandler = function(event) {
		var target = event.target;
		
		if (target.tagName === "LI") {
			slide.index = target.index;
			slide.reElement(target.index);
		}
	};
	
	slide.autoPlay = function() {
		clearInterval(slide.timer);
		slide.timer = setInterval(function() {
			slide.index++;
			if (slide.index >= slide.len) {
				slide.index = 0;
			}
			slide.reElement(slide.index);
		}, 3000);
	};
	slide.autoPlay();
	
	slide.navBox.addEventListener("mouseover", function(event) {
		slide.eventHandler(event);
	}, false);
	
	slide.frame.addEventListener("mouseover", function() {
		clearInterval(slide.timer);
	}, false);
	
	slide.frame.addEventListener("mouseout", function() {
		slide.autoPlay();
	}, false);
	
	slide.pre.addEventListener("click", function() {
		slide.index--;
		if (slide.index <= 0) {
			slide.index = slide.len - 1;
		}
		slide.reElement(slide.index);
	}, false);
	
	slide.next.addEventListener("click", function() {
		slide.index++;
		if (slide.index >= slide.len) {
			slide.index = 0;
		}
		slide.reElement(slide.index);
	}, false);
	
	//表单验证
	var done = {
		name: false,
		moblie: false,
		email: false,
		commName: false,
		jobTitle: false,
		money: false
	};
	
	//切换表单
	function reCheckbox(checkBox) {
		var elm = $(".select"),
			list = $(".list"),
			d2 = $(".d2"),
			d1 = $(".d1");
			
		if (checkBox.hasClass("t1")) {
			list.hide();
			elm.show();
			d2.hide();
			d1.show();
		} else {
			list.show();
			elm.hide();
			d2.show();
			d1.hide();
		}
	}
	doc_.on("click", ".t1", function() {
		reCheckbox($(this));
	});
	
	doc_.on("click", ".t2", function() {
		reCheckbox($(this));
	});
	
	doc_.on("click", ".pop_btn", function() {
		$(".pop").show();
		$(".mirror").show();
	});
	//隐藏表单
	doc_.on("click", ".close_", function() {
		$(".pop").hide();
		$(".mirror").hide();
	});
	doc_.on("click", ".mirror", function() {
		$(".pop").hide();
		$(".mirror").hide();
	});
	//检查姓名
	function checkName(elm, err, msg1, msg2, key, len) {
		var val = elm.val();
			
		if (utils.empty(val)) {
			utils.errorMessage(err, msg1);
			done[key] = false;
			return;
		}
		if (val.length > len) {
			utils.errorMessage(err, msg2);
			done[key] = false;
			return;
		}
		done[key] = true;
	}
		
	doc_.on("focus", ".name", function() {
		utils.clearErrMessage($(this), $(".name_err"));
	});
	
	//检查手机
	function checkMob() {
		var moblie = $(".moblie"),
			val = moblie.val();
			
		if (utils.empty(val)) {
			utils.errorMessage($(".mob_err"), "手机号不能为空！");
			done.moblie = false;
			return;
		}
		if (!utils.checkMoblie(moblie)) {
			utils.errorMessage($(".mob_err"), "请填写正确的手机号码！");
			done.moblie = false;
			return;
		}
		done.moblie = true;
	}
	
	//检查邮箱
	function checkEmail() {
		var email = $(".email"),
			val = email.val();
			
		if (!utils.checkEmail(val)) {
			utils.errorMessage($(".email_err"), "请填写正确的邮箱地址！");
			done.email = false;
		}
		done.email = true;
	}
	//检测金额
	function checkMoney(elm, err, msg1, msg2, key) {
		var val = elm.val();
		
		if (utils.empty(val)) {
			utils.errorMessage($(".money_err"), msg1);
			done.money = false;
			return;
		}
		
		if (!utils.isNumber(val)) {
			utils.errorMessage($(".money_err"), msg2);
			done[key] = false;
			return;
		}
		done[key] = true;
	}
	
	doc_.on("focus", ".moblie", function() {
		utils.clearErrMessage($(this), $(".mob_err"));
	});
	doc_.on("focus", ".commpany", function() {
		utils.clearErrMessage($(this), $(".comm_err"));
	});
	doc_.on("focus", ".job_title", function() {
		utils.clearErrMessage($(this), $(".job_err"));
	});
	doc_.on("focus", ".money", function() {
		utils.clearErrMessage($(this), $(".money_err"));
	});
	doc_.on("click", ".submit", function() {
		checkName($(".name"), $(".name_err"), "姓名不能为空！", "姓名长度限制为4个字符！", "name", 4);
		checkMob();
		checkEmail();
		checkName($(".commpany"), $(".comm_err"), "公司名称不能为空！", "公司名称长度限制为30个字符！", "commName", 30);
		checkName($(".job_title"), $(".job_err"), "职务名称不能为空！", "职务名称长度限制为10个字符！", "jobTitle", 10);
		checkMoney($(".money"), $(".money_err"), "投资金额不能为空！", "投资金额只能是数字！", "money");
		
		if (done.commName &&
			done.email &&
			done.jobTitle &&
			done.moblie &&
			done.money) {
			//todo
			
		}
	});

	
	//交互
	//滚动内容
	
	var M = {};
	M.ScrollElm = function() {
		var that = this;
		that.head_show = doc.querySelector(".head_show");
		that.finance = doc.querySelector(".finance");
		that.join = doc.querySelector(".jon");
		that.join_time = doc.querySelector(".join_time");
		that.activity = doc.querySelector(".activity");
		that.section = doc.querySelectorAll(".wrapp section");
		that.list = doc.querySelectorAll(".r_nav li");
	};
	se = M.se = new M.ScrollElm();
	
	se.setIndex = function() {
		var len = se.list.length,
			list = se.list;
		
		for (var i = 0; i < len; i++) {
			list[i].index = i;
		}
	};
	
	doc_.on("click", ".r_nav li", function() {
		var index = $(this).index(),
			list = Array.prototype.slice.call(se.list);
		window.scrollTo(0, se.section[index].offsetTop);
		list.forEach(function(item, i) {
			item.className = "";
		});
		list[index].className = "selected";
	});

});